//
//  MTDiscoverViewController.swift
//  MT
//
//  Created by ooops on 2020/5/14.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import YPNavigationBarTransition
import MTCategory
import RxSwift
import RxCocoa

enum DiscoverItemTypeStruct {
    /// 任务中心
    case taskCenter
    /// 破冰游戏
//    case breakIce
    /// 钻石奖池
    case pool
    /// 排行榜
    case rate
}

public struct DiscoverItemStruct {
    var itemIcon: String
    var itemName: String
    var isShowArrow: Bool
    var type: DiscoverItemTypeStruct
    
    mutating func create(itemIcon: String, itemName: String, isShowArrow: Bool, type: DiscoverItemTypeStruct) -> DiscoverItemStruct {
        self.itemName = itemName
        self.isShowArrow = isShowArrow
        self.type = type
        return self
    }
}

class MTDiscoverViewController: BaseViewController {
    
    private lazy var provider: MTProvider<MTDiscoverAPI> = {
        let provider = MTProvider<MTDiscoverAPI>(plugins: [MTLoadingDataPlugin.init(viewController: self)])
        return provider
    }()
    let dataSource: [DiscoverItemStruct] = [/*DiscoverItemStruct(itemIcon: "discover_game", itemName: "破冰游戏", isShowArrow: true, type: .breakIce),*/
        DiscoverItemStruct(itemIcon: "discover_mission", itemName: "任务中心", isShowArrow: true, type: .taskCenter),
        DiscoverItemStruct(itemIcon: "discover_prize", itemName: "钻石奖池", isShowArrow: true, type: .pool),
        DiscoverItemStruct(itemIcon: "discover_ranking", itemName: "排行榜", isShowArrow: true, type: .rate)]

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        loadRankingChampion()
    }
    
    lazy var footerView: MTDiscoverTableFooterView = {
        let v = MTDiscoverTableFooterView()
        v.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: v.imageWH + 20)
        v.delegate = self
        return v
    }()
    
    lazy var tableView: UITableView = {
        self.title = "发现"
        let table = UITableView(frame: .zero, style: .plain)
        table.delegate = self
        table.dataSource = self
        table.register(MTDiscoverTableViewCell.self, forCellReuseIdentifier: MTDiscoverTableViewCell.reuseIdentity())
        table.separatorStyle = .none
        table.tableFooterView = footerView
        table.separatorStyle = .none
        table.backgroundColor = UIColor(hex: 0xF4F4F4)
        return table
    }()
    
    private func loadRankingChampion() {
        MTDiscoverTool.rankingCahmpion(provider: provider, complete: {[weak self] (model) in
            guard let weakSelf = self else { return }
            if model?.code == 200 {
                weakSelf.footerView.model = model
            }
        }) { (error) in
            
        }
    }
}

extension MTDiscoverViewController: MTDiscoverTableFooterViewDelegate {
    func didTap(discover: MTDiscoverTableFooterView, type: MTDiscoverTableFooterViewType) {
        let v = MTRateNestViewController(type: type)
        self.navigationController?.pushViewController(v, animated: true)
    }
}

extension MTDiscoverViewController: UITableViewDelegate, UITableViewDataSource {
    
    //MARK: UITableViewDataSource
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MTDiscoverTableViewCell.reuseIdentity(), for: indexPath) as! MTDiscoverTableViewCell
        configureCell(cell: cell, forRowAt: indexPath)
        return cell
    }
    
    func configureCell(cell: MTDiscoverTableViewCell, forRowAt indexPath: IndexPath) {
        let data: DiscoverItemStruct = dataSource[indexPath.row]
        cell.data = data
    }
    
    //MARK: UITableViewDelegate
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let data: DiscoverItemStruct = dataSource[indexPath.row]
        if data.type == .taskCenter {
            self.navigationController?.pushViewController(MTPartyTaskNestViewController(), animated: true)
            return
        }
        
        if data.type == .rate {
            self.navigationController?.pushViewController(MTRateNestViewController(), animated: true)
            return
        }
        self.navigationController?.pushViewController(MTLuckyViewController(), animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 64
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
}

extension MTDiscoverViewController {
    override func yp_navigtionBarConfiguration() -> YPNavigationBarConfigurations {
        return [.backgroundStyleColor, .backgroundStyleOpaque, .showShadowImage]
    }
}
